﻿using Furion.DatabaseAccessor;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Text;

namespace Mrhd.Core.Entities
{
    /// <summary>
    /// 商品分类别
    /// </summary>
    [Table("Sys_ProductType")]
    [Comment("商品分类别")]
    public class ProductTypeEntity: Entity, IEntitySeedData<ProductTypeEntity>, IEntityTypeBuilder<ProductTypeEntity>
    {
        /// <summary>
        /// 分类名称
        /// </summary>
        [Required]
        [Comment("分类名称")]
        [MaxLength(32)]
        public string Name { get; set; }
        /// <summary>
        /// 排序码
        /// </summary>
        [Comment("排序码")]
        public int Sort { get; set; }
        /// <summary>
        /// 当前标识状态
        /// </summary>
        [Comment("当前标识状态")]
        public EntitySign Flag { get; set; }
        /// <summary>
        /// 所属分类产品
        /// </summary>
        [Comment("商品分类别")]
        public virtual ICollection<ProductEntity> Products { get; set; }


        /// <summary>
        /// Fluent Api
        /// </summary>
        /// <param name="entityBuilder"></param>
        /// <param name="dbContext"></param>
        /// <param name="dbContextLocator"></param>
        public void Configure(EntityTypeBuilder<ProductTypeEntity> entityBuilder, DbContext dbContext, Type dbContextLocator)
        {
            entityBuilder.HasMany(a => a.Products)
                .WithOne(a => a.ProductType)
                .HasForeignKey(a => a.ProductTypeId)
                .OnDelete(DeleteBehavior.SetNull);      // 当商品类型删除时，将所属的商品其类型属性设置为NULL（无需跟踪对象实体）
        }

        /// <summary>
        /// 种子
        /// </summary>
        /// <param name="dbContext"></param>
        /// <param name="dbContextLocator"></param>
        /// <returns></returns>
        public IEnumerable<ProductTypeEntity> HasData(DbContext dbContext, Type dbContextLocator)
        {
            var time = new DateTimeOffset(2021, 9, 21, 8, 0, 0, default);
            return new List<ProductTypeEntity>
            {
                new ProductTypeEntity { Id = 1, Name = "发热", CreatedTime = time, Flag = EntitySign.Add, Sort = 1 },
                new ProductTypeEntity { Id = 2, Name = "头昏", CreatedTime = time, Flag = EntitySign.Add, Sort = 2 },
                new ProductTypeEntity { Id = 3, Name = "发炎", CreatedTime = time, Flag = EntitySign.Add, Sort = 3 },
                new ProductTypeEntity { Id = 4, Name = "皮肤", CreatedTime = time, Flag = EntitySign.Add, Sort = 4 }
            };
        }

    }



}
